#!/usr/bin/py2
# -*- coding: utf-8 -*-
#encoding=utf-8

# @author Funsion Wu
# 初始化脚本，删除redis缓存，删除monngodb采集库，删除.pyc文件，删除已下载的图片

from funsion import settings
from optparse import OptionParser
from redis import Redis
from pymongo.mongo_client import MongoClient
import os, shutil

parser = OptionParser()
parser.add_option( '-n', '--name', dest='bot_name', default=settings.BOT_NAME, help=u'想要清空数据的目标爬虫名称' )
(options, args) = parser.parse_args()

rds = Redis(
    host = settings.REDIS_HOST,
    port = settings.REDIS_PORT,
)

print "\n------------------- 清除Redis信息 -------------------"
rds.delete(options.bot_name + ':dupefilter')
rds.delete(options.bot_name + ':items')
print options.bot_name + ':dupefilter Redis Key Delete'
print options.bot_name + ':items Redis Key Delete'

print "\n------------------- 清除MongoDB信息 -------------------"
connection = MongoClient(settings.MONGODB_URI)
database = connection[settings.MONGODB_DATABASE]
collection = database[settings.MONGODB_COLLECTION]
collection.drop()
print '%s:%s Mongodb Collection Delete' %(settings.MONGODB_DATABASE, settings.MONGODB_COLLECTION)

print "\n------------------- 清除临时文件 -------------------"
path = os.path.expanduser('./')
for (dirname, subdir, subfile) in os.walk(path):
    for file in subfile:
        filepath = os.path.join(dirname, file)
        if filepath.endswith(('.log', '.pyc')):
            os.remove(filepath)
            print filepath + ' File Delete'

print "\n------------------- 清除图片文件 -------------------"
if os.path.isdir(settings.IMAGES_STORE):
    shutil.rmtree(settings.IMAGES_STORE)
print settings.IMAGES_STORE + ' Directory Delete'

print
